home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Amiga / timerinit.c < prev    next >
C/C++ Source or Header  |  1998-12-25  |  7KB  |  263 lines

  1. RCS_ID_C="$Id: timerinit.c,v 4.3 1994/10/04 07:43:47 jraja Exp $";
  2. /*
  3.  *      timerinit.c - SAS C auto initialization functions for timer device
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. /****** net.lib/autoinit_timer.device *****************************************
  11.  
  12.     NAME
  13.         timerinit - SAS C Autoinitialization Functions for timer.device
  14.  
  15.     SYNOPSIS
  16.         #include <time.h>
  17.     
  18.         int daylight;
  19.         long timezone;
  20.         char *tzname[2];
  21.  
  22.         void tzset(void);
  23.  
  24.         #include <sys/time.h>
  25.  
  26.         struct Device *TimerBase;
  27.  
  28.         LONG _STI_200_openTimer(void);
  29.         void _STD_200_closeTimer(void);
  30.         
  31.     FUNCTION
  32.         These functions open and close the timer.device at the startup and
  33.         exit of the program, respectively. For a program to use these
  34.         functions, it must be linked with netlib:net.lib.
  35.  
  36.         The opened device base is stored in the TimerBase global variable.
  37.  
  38.         If the device can be opened, the _STIopenTimer() sets up the time zone
  39.         information, which is used by the gettimeofday() function and the time
  40.         conversion routines of the C-library.
  41.  
  42.     NOTES
  43.         The time zone information is got from the environment variable named
  44.         TZ. The format for this variable is:
  45.  
  46.             zzznnnddd
  47.  
  48.         where zzz is three letter identifier for the time zone (for example
  49.         GMT), and the nnn is hours west from Greenwich on range [-23,24]
  50.         (negative values are to east).  The last field is the abbreviation for
  51.         the local daylight saving time zone (which is not interpreted by this
  52.         version).
  53.  
  54.         If the TZ environment variable cannot be found, Greenwich Mean Time
  55.         (GMT) is used instead.
  56.  
  57.         The autoinitialization and autotermination functions are features
  58.         specific to the SAS C6.  However, these functions can be used with
  59.         other (ANSI) C compilers, too.  Example follows:
  60.  
  61.         \* at start of main() *\
  62.  
  63.         atexit(_STD_200_closeTimer);
  64.         _STI_200_openTimer();
  65.  
  66.     The tzset() does nothing. All the necessary initialization is done at
  67.         the autoinit function.
  68.  
  69.     BUGS
  70.         TZ "hours west from GMT" should be interpreted as float.
  71.  
  72.         The same autoinitialization won't work for both SAS C 6.3 and SAS C
  73.         6.50 or latter.  Only way to terminate an initialization function is
  74.         by exit() call with SAS C 6.3 binary.  If an autoinitialization
  75.         function is terminated by exit() call with SAS C 6.50 binary, the
  76.         autotermination functions won't be called.  Due this braindamage
  77.         these compilers require separate net.lib libraries.
  78.  
  79.     SEE ALSO
  80.         net.lib/gettimeofday(),
  81.         SAS/C 6 User's Guide p. 145 for details of autoinitialization and
  82.         autotermination functions.
  83. *****************************************************************************
  84. *
  85. */
  86.  
  87. #include <exec/types.h>
  88. #include <exec/devices.h>
  89. #include <dos/dos.h>
  90. #include <devices/timer.h>
  91. #include <proto/exec.h>
  92. #include <proto/dos.h>
  93. #include <string.h>
  94.  
  95. #include <stdlib.h>
  96. #include <stdio.h>
  97. #include <time.h>
  98. #include <sys/time.h>
  99.  
  100. #include <constructor.h>
  101.  
  102. /* SAS C 6.50 kludge */
  103. #if __VERSION__ > 6 || __REVISION__ >= 50
  104. #define exit(x) return(x)
  105. #endif
  106.  
  107. struct Device *TimerBase = 0L;
  108. static void *unit;
  109.  
  110. /*
  111.  * Time zone support for the gettimeofday. Zeroes default to the GMT
  112.  * without daylight saving.
  113.  */
  114. struct timezone __time_zone = {0,0};
  115.  
  116. /*
  117.  * Seconds to to the system time (seconds from 00:00 1.1.1978) 
  118.  * to the GMT (seconds from 00:00 1.1.1970).
  119.  * _STIopenTimer() adds the local time seconds west from GMT to this
  120.  * value, so the local time gets converted to the GMT.
  121.  */
  122. long __local_to_GMT = ((8L*365 + 8/4)*24*60*60);
  123.  
  124. int  __daylight = 0;
  125. long __timezone = 0;
  126. char *__tzname[2] = { 0 };
  127. char *_TZ = NULL;
  128.  
  129. char __zone_string[12] = "GMT0";
  130.  
  131. /* 
  132.  * Locale information is included here, since the 2.1 includes may be hard 
  133.  * to find.
  134.  */
  135.  
  136. /* This structure must only be allocated by locale.library and is READ-ONLY! */
  137. struct Locale
  138. {
  139.     STRPTR    loc_LocaleName;      /* locale's name         */
  140.     STRPTR    loc_LanguageName;      /* language of this locale     */
  141.     STRPTR    loc_PrefLanguages[10];      /* preferred languages     */
  142.     ULONG    loc_Flags;          /* always 0 for now         */
  143.  
  144.     ULONG    loc_CodeSet;          /* always 0 for now         */
  145.     ULONG    loc_CountryCode;      /* user's country code     */
  146.     ULONG    loc_TelephoneCode;      /* country's telephone code     */
  147.     LONG    loc_GMTOffset;          /* minutes from GMT         */
  148.  
  149. /* deleted the rest to save space */
  150. };
  151.  
  152. void CloseLocale( struct Locale *locale );
  153. struct Locale *OpenLocale( STRPTR name );
  154.  
  155. #pragma libcall LocaleBase CloseLocale 2A 801
  156. #pragma libcall LocaleBase OpenLocale 9C 801
  157.  
  158. //LONG __stdargs
  159. //_STI_200_openTimer(void)
  160. CONSTRUCTOR_P(openTimer,200)
  161. {
  162.   struct timerequest dummyTimer = { 0 };
  163.  
  164.   if (!TimerBase && !OpenDevice("timer.device", UNIT_VBLANK, 
  165.                 (struct IORequest *)&dummyTimer, 0L)) {
  166.     TimerBase = dummyTimer.tr_node.io_Device;
  167.     unit = dummyTimer.tr_node.io_Unit;
  168.     if (TimerBase->dd_Library.lib_Version >= 36) {
  169.       /*
  170.        * Initialize time zone information for the gettimeofday()
  171.        * First try to open locale (2.1 and up), and if that fails,
  172.        * try to read environment variable TZ.
  173.        */
  174.       void *LocaleBase;
  175.       struct Locale *thisLocale = NULL;
  176.       short dstoff = 0;
  177.  
  178.       if ((LocaleBase = OpenLibrary("locale.library", 38)) != NULL) {
  179.     if ((thisLocale = OpenLocale(NULL)) != NULL) {
  180.       /*
  181.        * Update time zone minutes west from GMT.
  182.        */
  183.       strcpy(__zone_string,"???0"); /* unknown timezone name */
  184.       __time_zone.tz_minuteswest = thisLocale->loc_GMTOffset;
  185.       CloseLocale(thisLocale);
  186.     }
  187.     CloseLibrary(LocaleBase);
  188.       }
  189.       if (!thisLocale) { /* if locale information was not available */
  190.     short len, i;
  191.     long value;
  192.     BPTR file = Open("ENV:TZ", MODE_OLDFILE);
  193.     if (file) {
  194.       len = Read(file, __zone_string, sizeof(__zone_string) - 1);
  195.       if (len > 3) {
  196.         /*
  197.          * make sure the string is 0 terminated and does not have the
  198.          * newline at the end
  199.          */
  200.         for (i = 0; i < len; i++)
  201.           if (__zone_string[i] < ' ')
  202.         break;
  203.         __zone_string[i] = '\0';
  204.  
  205.         /* should interpret floats as well! */
  206.         if ((dstoff = StrToLong(__zone_string+3, &value)) > 0) {
  207.           /*
  208.            * Update time zone minutes west from GMT.
  209.            */
  210.           __time_zone.tz_minuteswest = (short)value * (short)60;
  211.           /*
  212.            * Set the offset to the possible DST zone name
  213.            */
  214.           dstoff += 3;
  215.         }
  216.       }
  217.       Close(file);
  218.     }
  219.       }
  220.  
  221.       /*
  222.        * Update local time seconds to GMT translation
  223.        */
  224.       __timezone = (short)__time_zone.tz_minuteswest * (short)60;
  225.       __local_to_GMT += __timezone;
  226.  
  227.       /*
  228.        * tzset() stuff
  229.        */
  230.       if (dstoff > 3) {
  231.     __daylight = 1;
  232.       }
  233.       else
  234.     dstoff = 3;
  235.  
  236.       __zone_string[3] = '\0'; /* terminate time zone name */
  237.       __tzname[0] = __zone_string;
  238.       __tzname[1] = __zone_string + dstoff;
  239.  
  240.       return 0;
  241.     }
  242.   }
  243.   exit(20);
  244. }
  245.  
  246. //void __stdargs
  247. //_STD_200_closeTimer(void)
  248. DESTRUCTOR_P(closeTimer,200)
  249. {
  250.   struct timerequest dummyTimer = { 0 };
  251.   if (!TimerBase)
  252.     return;
  253.  
  254.   dummyTimer.tr_node.io_Device = TimerBase;
  255.   dummyTimer.tr_node.io_Unit = unit;
  256.   CloseDevice((struct IORequest*)&dummyTimer);
  257. }
  258.  
  259. void
  260. tzset(void)
  261. {
  262. }
  263.